#include "WidgetTree.h"
#include <WidgetsManager.h>
#include "DefineCommon.h"
#include "DialogTypeList.h"
#include "EControllerQtTree.h"
#include "EItem.h"
#include "qtreewidget.h"
#include "qtreewidgetitemiterator.h"
#include "ui_WidgetTree.h"
#include <EItemManager.h>
#include <ECommandManager.h>





WidgetTree::WidgetTree(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::WidgetTree)
{
    ui->setupUi(this);
    ui->treeWidget->clear();

    QObject::connect(ui->buttonAdd,
                     SIGNAL(clicked(bool)),
                     this,
                     SLOT(slotButtonAdd(bool))
                   );
    QObject::connect(ui->buttonCopy,
                     SIGNAL(clicked(bool)),
                     this,
                     SLOT(slotButtonCopy(bool))
                   );
    QObject::connect(ui->buttonDelete,
                     SIGNAL(clicked(bool)),
                     this,
                     SLOT(slotButtonDelete(bool))
                   );
    QObject::connect(ui->buttonRefresh,
                     SIGNAL(clicked(bool)),
                     this,
                     SLOT(slotButtonRefresh(bool))
                   );
    QObject::connect(ui->treeWidget,
                     SIGNAL(currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
                     this,
                     SLOT(slotSelectItem(QTreeWidgetItem*, QTreeWidgetItem*))
                   );
    QObject::connect(ui->treeWidget,
        SIGNAL(expanded(QModelIndex)),
        this,
        SLOT(slotExpanded( QModelIndex))
    );


    EControllerQtTreePtr controller = NewSmartPtr(EControllerQtTree)();
    controller->setTreeWidget(ui->treeWidget);
    controller->setRootItem(EItemManager::instance()->getItemTreeRoot());
    controller->setName("WidgetTreeModel");
    controller->refresh();
    this->m_controllerTree = controller;

    ECommandManager::instance()->registDelegate(
            MakeDelegate(&EController::handleCommand, *m_controllerTree)
            );
    // m_controllerTree->addCmdSenderList(this->getTag());

    // this->refresh();
    
    // m_rootItem = new WidgetTreeItem();
    // m_rootItem->setText(0, "Default-root");
    // ui->treeWidget->addTopLevelItem(m_rootItem);
    
    // WidgetVariantMatrix* wPtr = new WidgetVariantMatrix;
    // QListWidgetItem* item = new QListWidgetItem();
    // ui->listWidget->addItem(item);
    // ui->listWidget->setItemWidget(item, wPtr);
}

WidgetTree::~WidgetTree()
{
    delete ui;
}
   
void WidgetTree::showEvent(QShowEvent* event)
{
    // if(this->m_firstShowEvent)
    // {
        // this->refresh();
        // this->m_firstShowEvent = false;
    // }
}

QTreeWidgetItem* WidgetTree::getRootItem()
{
    return m_rootItem;
}

EControllerPtr WidgetTree::getController()
{
    return m_controllerTree;
}

void WidgetTree::setController(EControllerQtTreePtr controller)
{
    controller->setTreeWidget(ui->treeWidget);

    ECommandManager::instance()->unregistDelegate(this->m_controllerTree.get());
    this->m_controllerTree = controller;
}
    
long WidgetTree::getTag()
{
    return (long)this;
}

void WidgetTree::refresh()
{
    this->m_controllerTree->refresh();
}

void WidgetTree::expandAll()
{
    ui->treeWidget->expandAll();
}

void WidgetTree::hideTopButtons()
{
    ui->buttonAdd->setHidden(true);
    ui->buttonCopy->setHidden(true);
    ui->buttonDelete->setHidden(true);
    ui->buttonRefresh->setHidden(true);
}

void WidgetTree::slotButtonAdd(bool value)
{
    QWidget* tempW=WidgetsManager::instance()->getWidgetFromMap("DialogTypeList");
    DialogTypeList* dialog = dynamic_cast<DialogTypeList*>(tempW);
    if(dialog == NULL)
    {
        ELOG_ERROR("DialogTypeList's ptr is nullptr");
        return ;
    }

    dialog->exec();
    EItemPtr typeItem = dialog->getController()->getSelectedItem();
    if(typeItem == nullptr || dialog->getIfCancelled() == true)
    {
        ELOG_ERROR("DialogTypeList's selected item is nullptr");
        // QString selectedNme= typeItem->getName().data();
        // EString tempStr = typeItem->getOrCreatMeta()->getPrintStr();
        // ELOG_DEBUG("SelectedName=%s\ntypeItem's meta=%s"
                // ,selectedNme.toStdString().data()
                // ,tempStr.data());
        return ;
    }

    // long tagTypeItem = typeItem->getTag();
    EItemPtr newItem = this->m_controllerTree->addItem(typeItem);
    if(newItem == nullptr)
    {
        ELOG_ERROR("Failed to add item %s", typeItem->getName().data());
        return;
    }

}

void WidgetTree::slotButtonCopy(bool value)
{
    EItemPtr newItem = this->m_controllerTree->copyItem(this->m_controllerTree->getSelectedItem());

    if(newItem == nullptr)
    {
        ELOG_ERROR("Failed to add item %s", this->m_controllerTree->getSelectedItem()->getName().data());
        return;
    }

    // EControllerTreeItem* currentItem = (EControllerTreeItem*)ui->treeWidget->currentItem();
    // if(currentItem != nullptr)
    // {
        // QTreeWidgetItem* itemParent = currentItem->parent();
        // if(itemParent != NULL)
        // {
            // this->m_controllerTree->copyItem(currentItem->m_tag);
        // }
    // }
}

// void WidgetTree::slotButtonPaste(bool value)
// {
    // EControllerTreeItem* currentItem = (EControllerTreeItem*)ui->treeWidget->currentItem();
    // if(currentItem != nullptr)
    // {
        // QTreeWidgetItem* itemParent = currentItem->parent();
        // if(itemParent != NULL)
        // {
            // this->m_controllerTree->copyItem(currentItem->m_tag);
        // }
    // }
// }

void WidgetTree::slotButtonDelete(bool value)
{
    this->m_controllerTree->deleteItem();
    // EControllerTreeItem* currentItem = (EControllerTreeItem*)ui->treeWidget->currentItem();
    // if(currentItem != nullptr)
    // {
        // QTreeWidgetItem* itemParent = currentItem->parent();
        // if(itemParent != NULL)
        // {
            // this->m_controllerTree->deleteItem(currentItem->m_tag);
        // }
    // }
}

void WidgetTree::slotButtonRefresh(bool value)
{
    this->m_controllerTree->refresh();
    

    // push a Cmd and EControllerQtTree will handle it.
    // ECommandPtr newCmd = NewSmartPtr(ECommand)();
    // newCmd->setSender( this->getTag() );
    // newCmd->setSenderName("WidgetTree.");
    // newCmd->setName("c:refreshWidgetTree");
    // // newCmd->addChild(item);
    // newCmd->setIfEnableUndo(false);
    // ECommandManager::instance()->pushECommand(newCmd);

}

void WidgetTree::slotSelectItem(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{
    // if (previous != nullptr && ((WidgetTreeItem*)previous)->m_itemBasePtr != nullptr)
    // {
        // EItemPtr previousIBP = ((WidgetTreeItem*)previous)->m_itemBasePtr;
        // emit signalDisSelectItemBase(previousIBP);
    // }
    // else
    // {
        // std::cout << "error, WidgetTree::slotSelectItem, previous->m_itemBasePtr==nullptr\n";
    // }

    // if(((WidgetTreeItem*)current)->m_itemBasePtr != nullptr)
    // {
        // EItemPtr currentIBP = ((WidgetTreeItem*)current)->m_itemBasePtr;
        // emit signalSelectItemBase(currentIBP);
    // }
    // else
    // {
        // std::cout<<"error, WidgetTree::slotSelectItem, current->m_itemBasePtr==nullptr\n";
    // }

    // emit signalSelectItem(current, previous);
}

void WidgetTree::slotExpanded(QModelIndex index)
{
    ui->treeWidget->resizeColumnToContents(index.column());
}
